home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / metkit / dump.cpp < prev    next >
C/C++ Source or Header  |  1997-06-07  |  4KB  |  157 lines

  1. //  Copyright (C) 1996, 1997 Meta Four Software.  All rights reserved.
  2. //
  3. //  Datafile dump utility sample code
  4. //
  5. //! rev="$Id: dump.cpp,v 1.11 1997/06/08 10:46:12 jcw Exp $"
  6.  
  7. #include "m4kit.h"
  8.  
  9. #include <stdio.h>
  10.  
  11. #if defined (unix)
  12.     #define try
  13.     #define catch(x)    if (0)
  14. #endif     
  15.  
  16. #if defined (macintosh)
  17.     #include /**/ <console.h>
  18.     #define d4_InitMain(c,v)    c = ccommand(&v)
  19. #endif
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // Recursively display the entire view contents. The results shown do not
  23. // depend on file layout (free space, file positions, flat vs. on-demand).
  24.  
  25. static void ViewDisplay(const c4_View& v_, int l_ =0)
  26. {
  27.     c4_String types;
  28.     bool hasData = false, hasSubs = false;
  29.  
  30.         // display header info and collect all data types
  31.     printf("%*s VIEW %5d rows =", l_, "", v_.GetSize());
  32.     for (int n = 0; n < v_.NumProperties(); ++n)
  33.     {
  34.         c4_Property prop = v_.NthProperty(n);
  35.         char t = prop.Type();
  36.  
  37.         printf(" %s:%c", (const char*) prop.Name(), t);
  38.         
  39.         types += t;
  40.     
  41.         if (t == 'V')
  42.             hasSubs = true;
  43.         else
  44.             hasData = true;
  45.     }
  46.     printf("\n");
  47.  
  48.     for (int j = 0; j < v_.GetSize(); ++j)
  49.     {
  50.         if (hasData)    // data properties are all shown on the same line
  51.         {
  52.             printf("%*s %4d:", l_, "", j);
  53.             c4_RowRef r = v_[j];
  54.             c4_Bytes data;
  55.  
  56.             for (int k = 0; k < types.GetLength(); ++k)
  57.             {
  58.                 c4_Property p = v_.NthProperty(k);
  59.  
  60.                 switch (types[k])
  61.                 {
  62.                 case 'I':
  63.                     printf(" %ld", (long) ((c4_IntProp&) p) (r));
  64.                     break;
  65.  
  66.                 case 'F':
  67.                     printf(" %g", (double) ((c4_FloatProp&) p) (r));
  68.                     break;
  69.  
  70.                 case 'D':
  71.                     printf(" %.12g", (double) ((c4_DoubleProp&) p) (r));
  72.                     break;
  73.  
  74.                 case 'S':
  75.                     printf(" '%s'", (const char*) (c4_String)
  76.                                         ((c4_StringProp&) p) (r));
  77.                     break;
  78.  
  79.                 case 'B':
  80.                     (p (r)).GetData(data);
  81.                     printf(" (%db)", data.Size());
  82.                     break;
  83.  
  84.                 default:
  85.                     if (types[k] != 'V')
  86.                         printf(" (%c?)", types[k]);
  87.                 }
  88.             }
  89.  
  90.             printf("\n");
  91.         }
  92.  
  93.         if (hasSubs)    // subviews are then shown, each as a separate block
  94.         {
  95.             for (int k = 0; k < types.GetLength(); ++k)
  96.             {
  97.                 if (types[k] == 'V')
  98.                 {
  99.                     c4_Property prop = v_.NthProperty(k);
  100.  
  101.                     printf("%*s %4d: subview '%s'\n", l_, "", j,
  102.                             (const char*) prop.Name());
  103.  
  104.                     if (l_ > 0 || k > 0)
  105.                     {
  106.                         c4_ViewProp& vp = (c4_ViewProp&) prop;
  107.  
  108.                         ViewDisplay(vp (v_[j]), l_ + 2);
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117.  
  118. int main(int argc, char** argv)
  119. {
  120.     #ifdef d4_InitMain
  121.         d4_InitMain(argc, argv);
  122.     #endif
  123.  
  124.     const char* msg = 0;
  125.     
  126.     if (argc != 2)
  127.         fprintf(stderr, "Usage: DUMP file\n");
  128.     else
  129.         try
  130.         {
  131.             msg = "could not open data file";
  132.  
  133.             c4_Storage store (argv[1], false);
  134.  
  135.             msg = "file may be damaged";
  136.  
  137.             c4_View base = store.Contents().Container();
  138.  
  139.             printf("%s: %d properties\n  %s\n\n",
  140.                                     argv[1], base.NumProperties(),
  141.                                     (const char*) store.Description());
  142.             ViewDisplay(base);
  143.  
  144.             msg = 0;
  145.         }
  146.         catch (...)
  147.         {
  148.         }
  149.     
  150.     if (msg)
  151.         fprintf(stderr, "Abnormal termination, %s\n", msg);
  152.  
  153.     return msg ? 1 : 0;
  154. }
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157.